﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    public partial class FrmImageVerifyCode : Form
    {
        public string VerifyCode { get; private set; }

        public event EventHandler<RefreshImageEventArgs> RefreshImage;

        public Func<bool> ConfirmVerifyCode; 

        public FrmImageVerifyCode()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (RefreshImage != null)
            {
                var args = new RefreshImageEventArgs();
                RefreshImage(this, args);
                if (args.Img == null)
                {
                    UiUtil.ClearPictureBox(this.pictureBox1);
                }
                else
                {
                    this.pictureBox1.Image = args.Img;
                    this.pictureBox1.Size = args.Img.Size;
                }
            }
        }

        private void bntOk_Click(object sender, EventArgs e)
        {
            this.VerifyCode = this.textBox1.Text.Trim();
            if (ConfirmVerifyCode != null)
            {
                var result = ConfirmVerifyCode();
                if (!result)
                {
                    // 验证失败后，重新获取验证码，并且不退出窗体
                    pictureBox1_Click(null, null);
                    this.textBox1.Clear();
                    this.textBox1.Focus();
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
        }

        private void FrmImageVerifyCode_Load(object sender, EventArgs e)
        {
            pictureBox1_Click(null, null);
        }
    }
}
